package com.cgsrp.cgsrp_baby.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import com.cgsrp.cgsrp_baby.domain.Vaccine;
import com.cgsrp.cgsrp_baby.dao.VaccineMapper;
import com.cgsrp.cgsrp_baby.service.VaccineService;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;

@Service
public class VaccineServiceImpl implements VaccineService{

    @Resource
    private VaccineMapper vaccineMapper;

    @Resource
    private RedisTemplate redisTemplate;

    @Override
    public int deleteByPrimaryKey(Integer id) {
        Vaccine vaccine = vaccineMapper.selectByPrimaryKey(id);
        HashOperations ops = redisTemplate.opsForHash();
        ops.delete("vaccines"+vaccine.getBabyId(),vaccine.getId());
        return vaccineMapper.deleteByPrimaryKey(id);
    }

    @Override
    public boolean insert(Vaccine record) {
        Vaccine vaccine = vaccineMapper.selectByPrimaryKey(record.getId());
        HashOperations ops = redisTemplate.opsForHash();
        if (vaccine!=null){
            ops.delete("vaccines"+record.getBabyId(),record.getId());
        }
        if (vaccineMapper.insert(record)>0){
            return true;
        }else{
            return false;
        }
    }

    @Override
    public int insertSelective(Vaccine record) {
        return vaccineMapper.insertSelective(record);
    }

    @Override
    public Vaccine selectByPrimaryKey(Integer id) {
        return vaccineMapper.selectByPrimaryKey(id);
    }

    @Override
    public int updateByPrimaryKeySelective(Vaccine record) {
        return vaccineMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public int updateByPrimaryKey(Vaccine record) {
        return vaccineMapper.updateByPrimaryKey(record);
    }

    @Override
    public PageInfo<Vaccine> list(int[] babyIds, Integer curPage, Integer pageSize) {
        HashOperations ops = redisTemplate.opsForHash();
        ArrayList<Vaccine> list1 = new ArrayList<>();
        for (int babyId : babyIds) {
            List<Vaccine> vaccines = ops.values("vaccines"+babyId);
            if (vaccines.isEmpty()){
                List<Vaccine> list=vaccineMapper.list(babyId);
                for (Vaccine vaccine : list) {
                    ops.put("vaccines"+babyId,vaccine.getId(),vaccine);
                }
                vaccines=ops.values("vaccines"+babyId);
            }
            list1.addAll(vaccines);
        }
        list1.sort(Comparator.comparingInt(Vaccine::getId));
        List<Vaccine> list2=new ArrayList<>();
        if (pageSize<list1.size()-(curPage-1)*pageSize) {
            list2 = list1.subList((curPage - 1) * pageSize, (curPage - 1) * pageSize + pageSize);
        }else {
            list2 = list1.subList((curPage - 1) * pageSize, list1.size());
        }
        PageInfo<Vaccine> pageInfo = new PageInfo<>(list2);
        pageInfo.setTotal(list1.size());
        PageHelper.startPage(curPage, pageSize);
        return pageInfo;
    }

}
